// SPDX-FileCopyrightText: 2023-2025 CASTest Corporation Limited
// SPDX-FileCopyrightText: 2023-2025 Institute of Computing Technology, Chinese Academy of Sciences
// SPDX-License-Identifier: LGPL-v3

#ifndef ICTEST_ATPGCOMMONS_H
#define ICTEST_ATPGCOMMONS_H

#include "atpg/ATPGDefine.h"
#include "atpg/LogicDrivenTable.h"
#include "fault/SAF.h"
#include "netlist/PrimNetlist.h"
#include "pattern/Pattern.h"
#include "simulation/SimEngine.h"
#include "util/TimeUtil.h"
#include <fstream>

namespace ictest {

struct FreeFanoutRegion {
  int id = -1;
  // ffr fanouts stem
  Gate *stem = nullptr;
  std::vector<FreeFanoutRegion *> fanins;
  std::vector<FreeFanoutRegion *> fanouts;
};

class ATPGCommons {
public:
  ATPGCommons() : prim_(nullptr), sim_(nullptr) {}
  ~ATPGCommons() = default;

  void FindFreeFanoutRegion();
  void GetRegisterInfo();
  void MarkClkCone();

  // Common data structures.
public:
  PrimNetlist *prim_;
  SimEngine *sim_;
  // free fanout region
  std::vector<FreeFanoutRegion *> free_fanout_regions_;
  std::vector<int> gate_ffr_id_;
  // the index is ffr id, the value is fault id
  std::vector<int> fault_support_region_;
  std::vector<int> already_visited_ffr_;

  std::map<int, int> po_ids_;

public:
  std::map<Gate *, int> gate2scanchainid_;
  std::vector<bool> is_ppi_;
  std::vector<bool> is_observe_gate_;

  std::vector<int> is_seq_element_;
  std::vector<int> is_falling_edge_dff_;
  std::vector<int> is_clk_cone_;
  std::vector<bool> no_assign_;
};
} // namespace ictest

#endif // ICTEST_ATPGCOMMONS_H
